//
//  ViewController.m
//  Linkage
//
//  Created by administrator on 2017/8/31.
//  Copyright © 2017年 JohnLai. All rights reserved.
//

#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "UIColor+Tools.h"
#import "JohnTopTitleView.h"



const CGFloat headerH = 200;

@interface ViewController ()<JohnTopTitleViewDelegate>

@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) FirstViewController *firstVC;
@property (nonatomic,strong) SecondViewController *secondVC;
@property (nonatomic,strong) ThirdViewController *thirdVC;
@property (nonatomic,strong) JohnTopTitleView *topTitleView;

@property (nonatomic, assign) CGFloat currentContentOfSetY;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
    [self setting];
    
}

- (void)setting{
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kScreenW, headerH)];
    self.headerView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.headerView];
    
    [self.view addSubview:self.topTitleView];
}

#pragma mark - CustomDelegate
- (void)johnScrollViewDidScroll:(CGFloat)scrollY{
    _currentContentOfSetY = scrollY;
    NSLog(@"_currentContentOfSetY = %lf",_currentContentOfSetY);
    CGFloat headerViewY;
    if (scrollY > 0) {
       headerViewY = -scrollY + 64;
        if (scrollY > headerH) {
            headerViewY = -headerH + 64;
        }
    }else{
        headerViewY = 64;
    }
    self.headerView.frame = CGRectMake(0,headerViewY, kScreenW, headerH);
    self.topTitleView.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), kScreenW, kScreenH - CGRectGetMaxY(self.headerView.frame));
}

-(void)showLastState:(CGFloat)contentOfSetY
{
    
}

#pragma mark - JohnTopTitleViewDelegate
- (void)didSelectedPage:(NSInteger)page{
    self.headerView.frame = CGRectMake(0, 64, kScreenW, headerH);
    self.topTitleView.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), kScreenW, kScreenH - CGRectGetMaxY(self.headerView.frame));
    switch (page) {
        case 0:
        {
            CGFloat contentOfSetY = _currentContentOfSetY > headerH ? (self.firstVC.contentOfSetY > headerH ? self.firstVC.contentOfSetY : (self.firstVC.contentOfSetY > 0 ? headerH : 0)) : self.firstVC.contentOfSetY > 0 ? _currentContentOfSetY : 0;
            [self johnScrollViewDidScroll:contentOfSetY];
            [self.firstVC.tableView setContentOffset:CGPointMake(0, contentOfSetY) animated:NO];
        }
            break;
            case 1:
        {
            CGFloat contentOfSetY = _currentContentOfSetY > headerH ? (self.secondVC.contentOfSetY > headerH ? self.secondVC.contentOfSetY : (self.secondVC.contentOfSetY > 0 ? headerH : 0)) : self.secondVC.contentOfSetY > 0 ? _currentContentOfSetY : 0;
            [self johnScrollViewDidScroll:contentOfSetY];
            [self.secondVC.tableView setContentOffset:CGPointMake(0, contentOfSetY) animated:NO];
            
        }
            break;
        default:
        {
            NSLog(@"self.thirdVC.contentOfSetY = %lf",self.thirdVC.contentOfSetY);
            CGFloat contentOfSetY = _currentContentOfSetY > headerH ? (self.thirdVC.contentOfSetY > headerH ? self.thirdVC.contentOfSetY : (self.thirdVC.contentOfSetY > 0 ? headerH : 0)) : self.thirdVC.contentOfSetY > 0 ? _currentContentOfSetY : 0;
            [self johnScrollViewDidScroll:contentOfSetY];
            [self.thirdVC.collectionView setContentOffset:CGPointMake(0, contentOfSetY) animated:NO];
            
        }
            break;
    }
}

#pragma mark - Getter
- (JohnTopTitleView *)topTitleView{
    if (!_topTitleView) {
        _topTitleView = [[JohnTopTitleView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.headerView.frame), kScreenW, kScreenH - 64)];
        _topTitleView.titles = @[@"我的空间",@"我的访客",@"我访问的"];
        [_topTitleView setupViewControllerWithFatherVC:self childVC:@[self.firstVC,self.secondVC,self.thirdVC]];
        _topTitleView.delegete = self;
    }
    return _topTitleView;
}

- (FirstViewController *)firstVC{
    if (!_firstVC) {
        _firstVC = [[FirstViewController alloc]init];
        __weak typeof(self) weakSelf = self;
        _firstVC.DidScrollBlock = ^(CGFloat scrollY) {
            [weakSelf johnScrollViewDidScroll:scrollY];
        };
    }
    return _firstVC;
}

- (SecondViewController *)secondVC{
    if (!_secondVC) {
        _secondVC = [[SecondViewController alloc]init];
        __weak typeof(self) weakSelf = self;
        _secondVC.DidScrollBlock = ^(CGFloat scrollY) {
            [weakSelf johnScrollViewDidScroll:scrollY];
        };
    }
    return _secondVC;
}

- (ThirdViewController *)thirdVC{
    if (!_thirdVC) {
        _thirdVC = [[ThirdViewController alloc]init];
        __weak typeof(self) weakSelf = self;
        _thirdVC.DidScrollBlock = ^(CGFloat scrollY) {
            [weakSelf johnScrollViewDidScroll:scrollY];
        };
    }
    return _thirdVC;
}




@end
